﻿    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    using School.Entities;
    using System.Collections;
    using System.IO;

namespace School.Admission
{
    public class HomeAddress
    {
        private static SchoolEntities db = new SchoolEntities();

        public static long Add(SD_HomeAddress  homeaddress)
        {
            try
            {
                db.AddToSD_HomeAddress (homeaddress);
                db.SaveChanges();
               // return db.SD_HomeAddress.LastOrDefault().Id;
                return homeaddress.Id;
               
            }

            catch
            {
                return 0;

            }
        }

        public static SD_HomeAddress Single(long id)
        {
            return db.SD_HomeAddress.Single(x => x.Id == id);
        }


        //public static SD_HomeAddress SingleByFatherId(long fid)
        //{
        //    try
        //    {
        //        return db.SD_HomeAddress.Single(x => x.Id  == fid);
        //    }
        //    catch
        //    {
        //        return null;
        //    }
        //}

        public static SD_HomeAddress SingleByMotherId(long mid)
        {
            try
            {
                var m = (from x in db.SD_Mother where x.Id == mid select x.HomeAddress_Id).First();

                return (from x in db.SD_HomeAddress where x.Id == m select x).First();
            }
            catch
            {
                return null;
            }
        }


        public static SD_HomeAddress SingleByfatherId(long fid)
        {
            try
            {
                var m = (from x in db.SD_Father where x.Id == fid select x.HomeAddress_Id).First();

                return (from x in db.SD_HomeAddress where x.Id == m select x).First();
            }
            catch
            {
                return null;
            }
        }


        public static SD_HomeAddress SingleBycustodyId(long cid)
        {
            try
            {
                var m = (from x in db.SD_Custody  where x.Id == cid select x.HomeAddress_Id).First();

                return (from x in db.SD_HomeAddress where x.Id == m select x).First();
            }
            catch
            {
                return null;
            }
        }

        public static IEnumerable<SD_HomeAddress> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from homeaddress in db.SD_HomeAddress select homeaddress ;

            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "StreetName")
                {
                    query = query.Where(c => c.StreetName == item.Value);
                }

                             
            }
            return query.ToList();
        }

        public static void Update(SD_HomeAddress homeaddress)
        {
            SD_HomeAddress c = db.SD_HomeAddress.Single(x => x.Id == homeaddress.Id);


            c.Id = homeaddress.Id;
            c.BuildingNo = homeaddress.BuildingNo;
            c.FloorNo = homeaddress.FloorNo;
            c.FlatNo = homeaddress.FlatNo;
            c.StreetName = homeaddress.StreetName;
            c.Stat_Id = homeaddress.Stat_Id;
            c.District_Id = homeaddress.District_Id;
            c.PostCode = homeaddress.PostCode;
            c.Phone1 = homeaddress.Phone1;
            c.Phone2 = homeaddress.Phone2;
            c.Phone3 = homeaddress.Phone3;
            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.SD_HomeAddress.DeleteObject(db.SD_HomeAddress.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
